//鼠标控制事件
export default class MouseEvent {
  constructor(sweeper) {
    const scope = this
    this.sweeper = sweeper
    this.init()
    this.threeManager = sweeper.threeManager
    let controls = this.threeManager.controls
    let renderer = this.threeManager.renderer
    //移动端操作函数
    renderer.domElement.addEventListener('touchstart', function (e) {
      scope.onTouchStart({ event: e, scope: scope })
    })
    renderer.domElement.addEventListener('touchmove', function (e) {
      scope.onTouchMove({ event: e, scope: scope })
    })
    renderer.domElement.addEventListener('touchend', function (e) {
      scope.onTouchEnd({ event: e, scope: scope })
    })

    let sensitive = sweeper.settings.sensitive
    controls.rotateSpeed = parseFloat(sensitive.xz)
    controls.zoomSpeed = parseFloat(sensitive.sf)
    controls.panSpeed = parseFloat(sensitive.py)
  }
  init(){
    this.timer = null
    this.stamp = 0 //限制移动事件触发次数
    this.isDrag = false
  }
  onTouchMove({ event, scope }) {
    let mouse = scope.threeManager.mouse
    let winSize = scope.threeManager.winSize
    //记录鼠标的二维坐标
    mouse.x = (event.changedTouches[0].clientX / winSize.winWidth) * 2 - 1
    mouse.y = -(event.changedTouches[0].clientY / winSize.winHeight) * 2 + 1
    if (parseInt(event.timeStamp / 10) > parseInt(scope.stamp / 10)) {
      //一秒最多渲染十次
      scope.threeManager.mouseChange()
    }
    scope.stamp = event.timeStamp
    scope.isDrag = true
  }
  onTouchStart({ event, scope }) {
    scope.isDrag = false
    //长按事件监听
    if (scope.timer) {
      clearTimeout(scope.timer)
    }
    //判断是否开启长按插旗功能
    if(scope.sweeper.settings.flagPress){
      scope.timer = setTimeout(() => {
        let mouse = scope.threeManager.mouse
        let winSize = scope.threeManager.winSize
        let threeManager = scope.threeManager
        mouse.x = (event.changedTouches[0].clientX / winSize.winWidth) * 2 - 1
        mouse.y = -(event.changedTouches[0].clientY / winSize.winHeight) * 2 + 1
        scope.threeManager.mouseChange()
        if (!scope.isDrag) {
          //如果点中立方体
          if (threeManager.hoverMesh) {
            scope.handleClick(threeManager.hoverMesh, 'flag')
            scope.isDrag = true
          }
        }
      }, 500)
    }
    
  }
  onTouchEnd({ event, scope }) {
    let mouse = scope.threeManager.mouse
    let winSize = scope.threeManager.winSize
    /** hoverMesh后面会变,不能直接使用hoverMesh的指针 */
    // let hoverMesh = scope.threeManager.hoverMesh
    let threeManager = scope.threeManager
    let handleFlag = scope.sweeper.handleFlag
    let numData = scope.sweeper.numData
    let mode = scope.sweeper.mode
    //记录鼠标的二维坐标
    mouse.x = (event.changedTouches[0].clientX / winSize.winWidth) * 2 - 1
    mouse.y = -(event.changedTouches[0].clientY / winSize.winHeight) * 2 + 1
    scope.threeManager.mouseChange()
    if (!scope.isDrag) {
      //点击事件
      if (threeManager.hoverMesh) {
        //如果点中立方体
        if (!numData) {
          //初始化游戏
          mode.initGame(threeManager.hoverMesh)
        }
        //触发点击事件
        scope.handleClick(threeManager.hoverMesh, handleFlag)
      }
    }
    if (scope.timer) {
      clearTimeout(scope.timer)
    }
  }

  handleClick(meshObj, state) {
    if(!this.sweeper.settings.flagSchools && state=="flag"){
      //盲扫流不配插旗
      return
    }
    let hiddenOnce = []
    let counter = this.sweeper.counter
    let meshCover = this.sweeper.threeManager.meshCover
    let threeManager = this.sweeper.threeManager
    let musicCtrl = this.sweeper.musicCtrl
    let hiddenList = this.sweeper.hiddenList
    let mode = this.sweeper.mode
    let gameLine = this.sweeper.gameLine

    switch (state) {
      case 'dig': //挖掘
        if(meshObj.know && !meshObj.flag){
          mode.handleNum(meshObj)
          return
        }
        //如果被插旗或者已经挖过则不挖
        if (meshObj.flag || !meshObj.visible) {
          return
        }
        if (mode.handleDig(meshObj)) {
          //如果返回值为真则判断能否获胜
          if (!mode.automatic() && !gameLine.ifWin()) {
            musicCtrl.playGameAudio({ type: 'dig' })
          }
        }
        break
      case 'flag': //插旗
        if(meshObj.know && !meshObj.flag){
          mode.handleNum(meshObj)
          return
        }
        //如果已知的话不能插旗也不能取消
        if (meshObj.know) {
          return
        }
        if (meshObj.flag) {
          meshObj.material = threeManager.material.default
          meshObj.flag = false
          if (!isNaN(counter.countNum)) counter.countNum++
        } else {
          meshObj.material = meshCover.numCovers[0]
          meshObj.flag = true
          if (!isNaN(counter.countNum)) counter.countNum--
        }
        if(!mode.automatic() && !gameLine.ifWin()){
          musicCtrl.playGameAudio({ type: 'flag' })
        }
        break
      case 'hide': //隐藏
        hiddenOnce = []
        meshObj.visible = false
        hiddenOnce.push(meshObj)
        hiddenList.push(hiddenOnce)
        document.getElementById('hideBack').style.backgroundColor = 'white'
        if(!mode.automatic()){
          musicCtrl.playGameAudio({ type: 'hidden' })
        }
        break
    }
    
  }
}
